package Code;

public class Grid 
{
	private double[][] theGrid;
	private int worldX;
	private int worldY;
	private int occupancyWidth;
	private final double initialValue = 0.7;
	
	private DisplayPanel view;
	/********************************************************
	 * Construct the grid
	 * @param worldHeight the world y
	 * @param worldWidth the world x
	 * @param occWidth the width of the occupancy reading
	 * @param tankLoc	the current location of the tank
	 *******************************************************/
	public Grid (int worldHeight, int worldWidth, int occWidth)
	{
		worldX = worldWidth;
		worldY = worldHeight;
		occupancyWidth = occWidth;
		theGrid = new double[worldX][worldY];
		
		initializeGrid();
	}
	
	public void setView(DisplayPanel view) {
		this.view = view;
	}
	
	/*******************************************************
	 * Updates the values of a pt of the grid
	 * @param pt the pt to be updated	NOTE: pt cast to int
	 * @param value the new value of the pt
	 *****************************************************/
	public void updateValue(Point pt, double value)
	{
		theGrid[(int) pt.getX()][ (int) pt.getY()] = value;
	}
	
	/**************************************************
	 * Gets the value for a pt
	 * @param pt	the pt whose value is desired
	 * @return		the value of the pt
	 **************************************************/
	public double getValue(Point pt)
	{
		return theGrid[(int) pt.getX()][ (int) pt.getY()];
	}
	
	public double getValue(int x, int y) {
		return getValue(new Point(x, y));
	}
	
	/*************************************
	 * Get the entire grid
	 * @return the grid
	 *************************************/
	public double[][] getGrid()
	{
		return theGrid;
	}

	/****************************************
	 * Triggers drawing of the current grid
	 ***************************************/
	public void update(final Tank tank, Point goal) {
		if(this.view != null) 
		{
			this.view.redraw(tank, occupancyWidth, goal);
		}
	}
	
	
	/*******************************
	 * @return the world x dimension
	 */
	public int getWorldX()
	{
		return worldX;
	}
	
	/*******************************
	 * @return the world y dimension
	 */
	public int getWorldY()
	{
		return worldY;
	}
	
	/******************************************
	 * @return width of the occupancy square
	 *****************************************/
	public int getOccWidth()
	{
		return occupancyWidth;
	}
	
	private void initializeGrid() 
	{
		for (int i = 0; i < worldX; i++)
		{
			for (int j = 0; j < worldY; j++)
			{
				theGrid[i][j] = initialValue;
			}
		}
	}
}
